from socket import * 
import thread
import time

# ver 0.05
#echo multu-thread socket server for DH 
#st00nsa@gmail.com
    
class Server:
    def connection(self,con,addr,id):
        while 1:
            data = con.recv(self.BUFSIZ)
            if not data: 
                break
            else:
                thread.start_new_thread(self.gate, (data, id))
        con.close()
        del self.users[id]
        print "user %s : %s disconected " %(str(addr[0]),str(addr[1]))
    def gate(self,data,id): 
        cmds=data.split(chr(0))
        
        for cmd in cmds:
            if not cmd=="":
                if cmd=="ping"+chr(0) or cmd=="ping":
                    self.ping_s[id]=time.time()
                    self.sender(id,"ping")
                else:
                    if cmd=="pong"+chr(0) or cmd=="pong":
                        self.ping_f[id]=time.time()   
                        ping=self.ping_f[id]-self.ping_s[id] 
                        self.sender(id,ping)
                        print "ping to %s = %s" % (id,ping)
                    else:#send to all
                        self.send_to_all(cmd,id) 
                
                      
    def send_to_all(self,data,id):
        for user in self.users:
            if not user==id:
                self.sender(user,data)
                    
    def sender(self,id,msg): 
        while 1:
            if not self.queue[id]=="busy":
                self.queue[id]="busy"
                msg=str(msg)            #to str
                if not msg[-1]==chr(0):
                    msg+=chr(0)
                self.users[id][0].send(msg)
                self.queue[id]="free"
                print "send to "+str(id)+" "+msg
                break
                
            
            
            
            
    def __init__(self):
        self.main_loop()
                                   
    def main_loop(self):
        HOST = ''
        PORT = 8002    
        self.BUFSIZ = 1024
        ADDR = (HOST, PORT)
        serversock = socket(AF_INET, SOCK_STREAM)
        serversock.bind(ADDR)
        serversock.listen(5)
        self.user_id=1
        self.users={}
        self.queue={}
        self.ping_s={}
        self.ping_f={}
        while 1:
            print 'hi im Bob ! I`m waiting ur command on port %s' % PORT
            clientsock, addr = serversock.accept()
            print 'hmm, some connection ',addr
            thread.start_new_thread(self.new_player, (clientsock, addr))
            
            
    def new_player(self,clientsock,addr):
        id=self.user_id
        self.user_id+=1
        self.users[id]=(clientsock,addr)
        self.queue[id]="free"
        for user in self.users:
            if not user==id:
                self.sender(id,"join&"+str(user))
        self.sender(id,"s&"+str(id))
        self.send_to_all("join&"+str(id),id)
        self.connection(clientsock, addr,id)
        
        
        
    
         
#server init auto start           
Server=Server()
    
